<?php
// +----------------------------------------------------------------------
// | 用户模块
// +----------------------------------------------------------------------

namespace app\user\model;

use think\Model as ThinkModel;
use util\Tree;
use think\facade\Db;

/**
 * 地区模型
 * @package app\area\model
 */
class Area extends ThinkModel
{
    // 设置当前模型对应的完整数据表名称
    protected $name = 'user_area';

    // 自动写入时间戳
    protected $autoWriteTimestamp = true;
	
	// 错误信息
	protected $error = '';
	
	/**
	 * 获取地区列表
	 * @return array|mixed
	 */
	public static function getColumn()
	{
	    $data_list = cache('area_column');
	    if (!$data_list) {
	        $data_list = self::getChilds();
			
	        // 非开发模式，缓存数据
	        if (config('app.develop_mode') == 0) {
	            cache('area_column', $data_list);
	        }
	    }
	    return $data_list;
	}
	
	
    /**
     * 获取指定类型数据
     * @param int $cid 类型id
     * @return mixed|static
     */
    public static function getChilds()
    {
        $data_list = self::where('pid', 0)->select();
		$list = [];
		foreach ($data_list as $k => $v) {
		    $list[$k] = [
				'value' => $v['id'],
				'label'	=> $v['name'],
				'children'	=> [],
			];
			$child = self::where('pid', $v['id'])->select();
			foreach ($child as $key => $value) {
				$list[$k]['children'][$key] = [
					'value' => $value['id'],
					'label'	=> $value['name'],
					'children'	=> [],
				];
				$children = self::where('pid', $value['id'])->select();
				foreach ($children as $ke => $val) {
					$list[$k]['children'][$key]['children'][$ke] = [
						'value' => $val['id'],
						'label'	=> $val['name'],
					];
				}
			}
		}
		return $list;
    }
	
	/**
	 * 保存请求数据
	 * @param array $data 数据
	 * @return mixed|static
	 */
	public function saveByApi($data_list = null, $pid = 0)
	{
	    if ($data_list == null){
			$this->error = '数据不能为空';
			return false;
		}
		if(!is_array($data_list)){
		    $data_list = (array)$data_list;
		}
		$save_list = [];
		foreach($data_list[0] as $k => $v) {
			// 一级地区数据
			$save_list[] = [
				'id'	=> $v['id'],
				'pid'	=> 0,
				'name'	=> $v['name'],
				'lat' 	=> $v['location']['lat'],
				'lng' 	=> $v['location']['lng'],
			];
			// 二级数据
			if (!empty($v['cidx'])) {
				foreach($data_list[1] as $key => $value) {
					if ($key >= $v['cidx'][0] && $key <= $v['cidx'][1]) {
						$save_list[] = [
							'id'	=> $value['id'],
							'pid'	=> $v['id'],
							'name'	=> $value['name'],
							'lat' 	=> $value['location']['lat'],
							'lng' 	=> $value['location']['lng'],
						];
						// 三级数据
						if (!empty($value['cidx'])) {
							foreach($data_list[2] as $ke => $val) {
								if ($ke >= $value['cidx'][0] && $ke <= $value['cidx'][1]) {
									if (empty($val['name'])) $val['name'] = $val['fullname'];
									$save_list[] = [
										'id'	=> $val['id'],
										'pid'	=> $value['id'],
										'name'	=> $val['name'],
										'lat' 	=> $val['location']['lat'],
										'lng' 	=> $val['location']['lng'],
									];
								}
							}
						}
					}
				}
			}
		}
		if ($save_list) {
			$area = new self;
			return $area->saveAll($save_list);
		} else {
			return false;
		}
	}

	/**
	 * 获取完整名称
	 * @param int $id id
	 * @author paulxun
	 * @return array
	 */
	public static function getFullName($id = 0)
	{
	    $info = self::where('id', $id)->find();
		if (!$info) {
			$title = '区域管理';
		} else {
			$title = $info['name'];
			$p = self::where('id', $info['pid'])->value('name');
			if ($p) $title = $p.'_'.$title;
		}
		
	    return $title;
	}
	/**
	 * 获取地址名称
	 * @param int $id id
	 * @author paulxun
	 * @return array
	 */
	public static function name($id = 0)
	{
	    $info = self::where('id', $id)->find();
		if (!$info) {
			$title = '';
		} else {
			$title = $info['name'];
		}
		
	    return $title;
	}
	/**
	 * 生成签名
	 * @param array $data 参与签名的数据
	 * @param string $str 分隔符
	 * @author Paulxun
	 * @return string
	 */
	private function auth_sign($data = [], $str = '@#')
	{
		// 数据类型检测
		if(!is_array($data)){
		    $data = (array)$data;
		}
		
		// 数组转换字符串
		$buff = implode($str, $data);
		
		//MD5加密签名
		$sign = md5($buff);
		
		// 返回大写字符串
		return strtoupper($sign);
	}
}